/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.model;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import edu.syr.iis.web.util.HibernateUtil;

/**
 * @author kinoue
 *
 */
public class DialogueActSchemeManager
{
    /**
     * Returns list of all DialogueActScheme database records sorted by daasetId
     */
    public List<DialogueActScheme> getDialogueActSchemes()
    {
	List<DialogueActScheme> list = null;
	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	session.beginTransaction();
	
	try
	{
	    list = session.createQuery("FROM DialogueActScheme ORDER BY dialogueActSchemeId").list();
	    session.getTransaction().commit();
	}
	catch (HibernateException e)
	{
	    session.getTransaction().rollback();
	    throw e;
	}
	return list;
    }
    
    /**
     * Returns list of all DialogueActScheme database records sorted by daasetId
     */
    public List<DialogueActScheme> getDialogueActSchemesDesc()
    {
	List<DialogueActScheme> list = null;
	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	session.beginTransaction();
	
	try
	{
	    list = session.createQuery("FROM DialogueActScheme ORDER BY dialogueActSchemeId DESC").list();
	    session.getTransaction().commit();
	}
	catch (HibernateException e)
	{
	    session.getTransaction().rollback();
	    throw e;
	}
	return list;
    }
    
    
    public DialogueActScheme getDialogueActScheme(int dialogueActSchemeId)
    {
	DialogueActScheme dialogueActScheme = null;
	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	session.beginTransaction();
	
	try
	{
	    dialogueActScheme = (DialogueActScheme) session.createQuery(
		    "FROM DialogueActScheme " +
		    " WHERE dialogueActSchemeId = ?").setInteger(0, dialogueActSchemeId).uniqueResult();
	    session.getTransaction().commit();
	}
	catch (HibernateException e)
	{
	    session.getTransaction().rollback();
	    throw e;
	}
	return dialogueActScheme;
    }
    
    /**
     * Saves a DialogueActScheme object.
     */
    public void saveDialogueActScheme(DialogueActScheme dialogueActScheme)
    {
        Session session = HibernateUtil.getSessionFactory()
                .getCurrentSession();
        session.beginTransaction();
        try
        {
            session.saveOrUpdate(dialogueActScheme);
            session.getTransaction().commit();
        }
        catch (HibernateException e)
        {
            session.getTransaction().rollback();
            throw e;
        }
    }
    
    /**
     * Deletes DialogueActScheme record with matching dialogueActSchemeId 
     */
    public void deleteDialogueActScheme(int dialogueActSchemeId)
    {
        Session session = 
            	HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();	
        try
        {
            Object toDelete = session.get(DialogueActScheme.class,new Integer(dialogueActSchemeId));
            
            if (toDelete != null) { 
        	session.delete(toDelete);
            }
            
            session.flush();
            session.getTransaction().commit();
        }
        catch (HibernateException e)
        {
            session.getTransaction().rollback();
            throw e;
        }
    }
}
